package com.hongye.search.service.impl;

import com.alibaba.fastjson.JSON;
import com.hongye.common.vo.RequirementVo;
import com.hongye.search.config.HongYeESConfig;
import com.hongye.search.service.RequirementService;
import com.hongye.search.vo.RequirementSearchParam;
import com.hongye.search.vo.RequirementSearchResult;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author lins
 * @date 2021/10/21 20:44
 */
@Service
public class RequirementServiceImpl implements RequirementService {
    @SuppressWarnings("all")
    @Autowired
    RestHighLevelClient client;
    @Override
    public RequirementSearchResult searchRequirement(RequirementSearchParam param) {
        SearchRequest searchRequest = buildRequirementSearch(param);
        System.out.println(searchRequest.source().toString());
        RequirementSearchResult searchResult = null;
        try {
            SearchResponse response = client.search(searchRequest, HongYeESConfig.COMMON_OPTIONS);
            searchResult = buildRequirementResult(response,param);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return searchResult;
    }

    private RequirementSearchResult buildRequirementResult(SearchResponse response, RequirementSearchParam param) {
        //region 封装解锁请求
        SearchHits responseHits = response.getHits();
        SearchHit[] hits = responseHits.getHits();
        List<RequirementVo> requirementVos = new ArrayList<>();
        for (SearchHit hit : hits) {
            RequirementVo requirementVo = JSON.parseObject(hit.getSourceAsString(), RequirementVo.class);
            requirementVos.add(requirementVo);
        }
        RequirementSearchResult searchResult = new RequirementSearchResult();
        searchResult.setRequirementVos(requirementVos);
        long value = responseHits.getTotalHits().value;
        searchResult.setTotal(value);
        searchResult.setPageNum(param.getPageNum());
        searchResult.setTotalPage(value%param.getSize()==0?(int)value/param.getSize():(int)value/param.getSize()+1);
        return searchResult;
        //endregion
    }

    private SearchRequest buildRequirementSearch(RequirementSearchParam param) {
        //region 构建检索请求
        SearchRequest searchRequest = new SearchRequest("requirement");
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        sourceBuilder.from((param.getPageNum() - 1) * param.getSize());
        sourceBuilder.size(param.getSize());
        BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
        if (param.getRequirementArea()!=null){
            boolQuery.must(QueryBuilders.termQuery("requirementArea", param.getRequirementArea()));
        }
        if (param.getRequirementHometype()!=null){
            boolQuery.must(QueryBuilders.termQuery("requirementHometype", param.getRequirementHometype()));
        }
        sourceBuilder.query(boolQuery);
        searchRequest.source(sourceBuilder);
        return searchRequest;
        //endregion
    }
}
